package com.mindainfo.groundwave.server.equipment.service;

import com.mindainfo.groundwave.common.api.equipment.param.TerminalQueryListParam;
import com.mindainfo.groundwave.common.api.equipment.service.ITerminalService;
import com.mindainfo.groundwave.common.api.equipment.vo.TerminalQueryListVo;
import com.mindainfo.groundwave.common.api.member.service.IDeptService;
import com.mindainfo.groundwave.common.bean.CommonResult;
import com.mindainfo.groundwave.common.bean.ResultFactory;
import com.mindainfo.groundwave.common.bean.TableData;
import com.mindainfo.groundwave.server.equipment.bean.TerminalInfo;
import com.mindainfo.groundwave.server.equipment.bean.TerminalInstall;
import com.mindainfo.groundwave.server.equipment.mapper.TerminalInfoMapper;
import com.mindainfo.groundwave.server.equipment.mapper.TerminalInstallMapper;
import org.apache.logging.log4j.core.util.datetime.FastDateFormat;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by MD-01 on 2017/7/7.
 */
@Service
@Transactional
public class TerminalSeviceImpl implements ITerminalService {

    @Resource
    private TerminalInfoMapper terminalInfoMapper;
    @Resource
    private TerminalInstallMapper terminalInstallMapper;
    @Resource
    private IDeptService deptService;


    /**
     * 页面查询列表
     */
    public CommonResult<TableData<TerminalQueryListVo>> queryList(TerminalQueryListParam param){

        //获取当前用户的部门树
        CommonResult<List<Integer>> myDeptIdList = deptService.getMyDeptIdList();
        List<TerminalInfo> infos = terminalInfoMapper.queryList(param.getStart(),
                param.getNum(),param.getKey(),myDeptIdList.getData());
        int count = terminalInfoMapper.queryListCount(param.getKey(),myDeptIdList.getData());

        List<TerminalQueryListVo> vos = new ArrayList<>();
        FastDateFormat format = FastDateFormat.getInstance("yyyy-MM-dd");
        for(TerminalInfo info : infos){
            TerminalQueryListVo vo = new TerminalQueryListVo();
            vo.setTerminalId(info.getId());
            vo.setRelayId(info.getRelayId());
            vo.setDeviceId(info.getDeviceId());
            vo.setVendor(info.getVendor());
            vo.setTerminalVersion(info.getVersion());
            vo.setMakerTime(format.format(info.getMakerTime()));

            TerminalInstall terminalInstall = terminalInstallMapper.queryByTerminalId(info.getId());
            if(terminalInstall!=null){
                vo.setInstallPersion(terminalInstall.getInstallPersion());
                vo.setInstallTime(format.format(terminalInstall.getInstallTime()));
                vo.setLocation(terminalInstall.getLocation());
                vo.setLng(terminalInstall.getLng());
                vo.setLat(terminalInstall.getLat());
            }
            vo.setPower(85);
            vo.setState("ok");
            vos.add(vo);
        }

        TableData<TerminalQueryListVo> data = new TableData<>();
        data.setData(vos);
        data.setRecordsTotal(count);
        data.setRecordsFiltered(count);
        return ResultFactory.ok(data);
    }
}
